package com.small.common.util.jwt.config;

import com.small.common.util.jwt.JwtHelper;
import com.small.common.util.jwt.info.IJwtInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.http.HttpServletRequest;

/**
 * 作者: fenny
 * 邮箱: 893146555@qq.com
 * 时间: 2017/10/22 08:35
 * 描述: 用户鉴权配置->解析token
 */
public class UserAuthConfig {

    @Value("${jwt.pub-key.path}")
    private String pubKeyPath;

    @Value("${jwt.header}")
    private String tokenHeader;

    public String getTokenHeader() {
        return tokenHeader;
    }

    public void setTokenHeader(String tokenHeader) {
        this.tokenHeader = tokenHeader;
    }

    public String getPubKeyPath() {
        return pubKeyPath;
    }

    public void setPubKeyPath(String pubKeyPath) {
        this.pubKeyPath = pubKeyPath;
    }

    public IJwtInfo getToken(HttpServletRequest request) throws Exception {
        //获取head中的token
        String authToken = request.getHeader(tokenHeader);
        //如果head中没有token获取参数中的
        if (StringUtils.isBlank(authToken)) {
            authToken = request.getParameter(tokenHeader);

        }
        return JwtHelper.getTokenInfo(authToken, pubKeyPath);
    }

}
